package com.chl.legou.order.listener;

import com.chl.legou.order.po.Order;
import com.chl.legou.order.service.OrderService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

/**
 * @author chlong
 * @date 2022/3/16
 */
@Component
@RabbitListener(queues = "${mq.order.queue.dlx}")
public class OrderLazyListener {
    @Autowired
    private OrderService orderService;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private Environment env;

    @RabbitHandler
    public void handlerData(String msg){
        System.err.println(msg);
        if (StringUtils.isEmpty(msg)){
            Long id = Long.parseLong(msg);
            Order order = orderService.getById(id);
            order.setOrderStatus("3");
            orderService.updateById(order); //修改订单状态
            //TODO 回滚库存
            rabbitTemplate.convertAndSend(env.getProperty("mq.order.exchange.ttl"),env.getProperty("mq.order.routing.ttl"),order.getId().toString());
        }
    }
}
